package cn.com.coding.app;

import cn.com.coding.common.lang.ResT;
import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.system.domin.ProductImageEntity;
import cn.com.coding.system.domin.ProductInfoEntity;
import cn.com.coding.system.domin.dto.AddProductDto;
import cn.com.coding.system.service.ProductInfoService;
import cn.hutool.core.map.MapUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 景点表
 *
 * @author mxz
 * @email codingce@gmail.com
 * @date 2022-03-05 14:01:22
 */
@RestController
@RequestMapping("cat/productInfo")
public class ProductInfoController extends BaseController {

    @Autowired
    private ProductInfoService productInfoService;

    /**
     * 景点分页
     */
    @RequestMapping("/getAllProduct")
    public ResT list(@RequestBody Map<String, Object> params) {
        PageUtil page = productInfoService.queryPage(params);

        return ResT.ok().put("productPage", page);
    }

    /**
     * 获取求购
     */
    @RequestMapping("/getAllBuy")
    public ResT getAllBuys(@RequestBody Map<String, Object> params) {
        PageUtil page = productInfoService.getAllBuy(params);
        return ResT.ok().put("buys", page);
    }

    /**
     * 热门商品
     */
    @RequestMapping(value = "/getHotProduct", method = RequestMethod.POST)
    public ResT getHotProduct(@RequestBody Map<String, Object> params) {
        PageUtil page = productInfoService.getHotProduct(params);

        return ResT.ok().put("productPage", page);
    }

    /**
     * getProductByAi
     */
    @RequestMapping("/getProductByAi")
    public ResT getProductByAi(@RequestBody Map<String, Object> params) {
        PageUtil page = productInfoService.getHotProduct(params);

        return ResT.ok().put("productPage", page);
    }

    /**
     * 用户分类查询
     *
     * @param params
     * @return
     */
    @RequestMapping("/getProductByCategory")
    public ResT getProductByCategory(@RequestBody Map<String, Object> params) {
        PageUtil page = productInfoService.queryPageByCategory(params);

        return ResT.ok().put("productPage", page);
    }

    /**
     * 发布
     */
    @RequestMapping("/release")
    public ResT release(@RequestBody AddProductDto addProductDto) {

        ProductInfoEntity productInfo = new ProductInfoEntity();
        BeanUtils.copyProperties(addProductDto, productInfo);

        //  后期用添加商品的自定义dto
        productInfo.setCategoryPid(addProductDto.getCatelogName());

        productInfoService.save(productInfo);

        if (addProductDto.getImgUrl() != null) {
            List<ProductImageEntity> list = addProductDto.getImgUrl();
            list.forEach(e -> {
                e.setProductPid(productInfo.getPid());
                productImageService.saveOrUpdate(e);
            });
        }

        return ResT.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{pid}")
    public ResT info(@PathVariable("pid") String pid) {
        ProductInfoEntity productInfo = productInfoService.getById(pid);
        productInfo.setViews(productInfo.getViews() + 1);
        productInfoService.updateById(productInfo);
        return ResT.ok().setData(MapUtil.builder().put("productInfo", productInfo).build());
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResT save(@RequestBody ProductInfoEntity goodInfo) {

        productInfoService.save(goodInfo);

        return ResT.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResT update(@RequestBody ProductInfoEntity productInfo) {

        productInfo.setUpdateTime(new Date());
        productInfoService.updateById(productInfo);

        return ResT.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResT delete(@RequestBody String[] pids) {
        List<ProductInfoEntity> list = new ArrayList<>(pids.length);

        Arrays.stream(pids).forEach(e -> {
            ProductInfoEntity goodInfoEntity = productInfoService.getById(e);
            goodInfoEntity.setProductStatus(4);
            goodInfoEntity.setUpdateTime(new Date());
            list.add(goodInfoEntity);
        });

        productInfoService.saveOrUpdateBatch(list);

        return ResT.ok();
    }

}